package com.tbit.uqbike.client.handler.borrowbikehandler;

import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.client.common.ResultMsg;
import com.tbit.uqbike.client.dao.core.BorrowCheckConfigLogDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.AccountConfig;
import com.tbit.uqbike.client.pojo.RideLog;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.borrowbike.BorrowCheckConfigLog;
import com.tbit.uqbike.client.service.RideLogService;
import com.tbit.uqbike.client.service.UserService;
import com.tbit.uqbike.client.service.XycxBorrowCheckService;
import com.tbit.uqbike.client.util.IdCardUtils;
import com.tbit.uqbike.client.vo.borrow.BorrowBikeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lh2
 * @ClassName: IdentityCheckHandler
 * @Description: 用户身份认证处理器
 * @date 2023-07-26 05:49:53
 */
@Slf4j
@Component
public class IdentityCheckHandler extends AbstractCheckHandler {
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private XycxBorrowCheckService xycxBorrowCheckService;
    @Autowired
    private BorrowCheckConfigLogDao borrowCheckConfigLogMapper;

    @Override
    public ResultMsg handle(BorrowBikeVO vo, String serialNO) {
        log.info("【用户身份认证处理器】校验开始...");
        // 降级：如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown() == 1) {
            log.info("【用户身份认证处理器】被降级，已自动跳过！");
            return super.next(vo, serialNO);
        }

        User user = vo.getUser();
        User userdb = userService.getByUserId(user.getUserId());
        AccountConfig accountConfig = vo.getAccountConfig();
        Integer age = IdCardUtils.getAgeForIdcard(userdb.getIdNO());
        if (age > 65) {
            throw new BaseException("-3057", "用户年龄超出65周岁，为了您身体安全，限制用车");
        }

        Integer rideNum = 0;
        List<RideLog> rideLogs = rideLogService.getByUserId(user.getUserId());
        if (rideLogs.size() > 0) {
            rideNum = rideLogs.size();
        }
        /** 判断是否已经实名认证 */
        /** 兼0容区域代理不开启强制实名认证 兼容区域设置免实名认证次数 */
        if (accountConfig != null && accountConfig.getNameAuth() == 1
                && (user.getNameAuth() == null || !user.getNameAuth())
                && rideNum >= accountConfig.getNameAuthFreeTimes()) {
            return ResultMsg.fail(-3024, "请先实名认证", TraceUtil.getTraceIdByLocal());
        }
        /** 判断是否已经学生认证 */
        /** 兼容区域代理开启强制学生认证 */
        if (accountConfig != null && accountConfig.getStudentAuth() == 1
                && (user.getStudentAuth() == null || !user.getStudentAuth())) {
            return ResultMsg.fail(-3040, "请先学生认证",TraceUtil.getTraceIdByLocal());
        }
        /** 小雨出行武汉大学开锁校验 */
        if (vo.getMachine().getAccountId().equals(11416) && userdb.getAccountId().equals(348) && !user.getStudentAuth()) {
            xycxBorrowCheckService.checkBorrow(user.getIdNO(), user.getUserId());
        }

        log.info("【用户身份认证处理器】校验通过！");
        BorrowCheckConfigLog bccLog = borrowCheckConfigLogMapper.findByNameAndSerialNO(
                this.getConfig().getHandlerName(), serialNO);
        if (bccLog != null) {
            bccLog.setExecuteResult(1);
            borrowCheckConfigLogMapper.updateByPrimaryKeySelective(bccLog);
        }
        // 执行下一个处理器
        return super.next(vo, serialNO);
    }
}
